package com.nq.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nq.common.ServerResponse;
import com.nq.dao.DomainConfigMapper;
import com.nq.pojo.DomainConfig;
import com.nq.service.IDomainConfigService;
import lombok.val;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DomainConfigServiceImpl implements IDomainConfigService {

    @Autowired
    private DomainConfigMapper domainConfigMapper;

    @Override
    public ServerResponse add(DomainConfig domainConfig) {
        if (null == domainConfig) {
            return ServerResponse.createByErrorMsg("Parameters cannot be empty.");
        }
        if (StringUtils.isBlank(domainConfig.getUrl())) {
            return ServerResponse.createByErrorMsg("url cannot be empty.");
        }
        domainConfig.setCreateTime(new Date());
        int count = domainConfigMapper.insert(domainConfig);
        if (count > 0) {
            return ServerResponse.createBySuccessMsg("Added successfully");
        }
        return ServerResponse.createByErrorMsg("Failed to add");
    }


    @Override
    public ServerResponse update(DomainConfig domainConfig) {
        if (null == domainConfig) {
            return ServerResponse.createByErrorMsg("Parameters cannot be empty.");
        }
        if (StringUtils.isBlank(domainConfig.getUrl())) {
            return ServerResponse.createByErrorMsg("url cannot be empty.");
        }
        int count = domainConfigMapper.updateById(domainConfig);
        if (count > 0) {
            return ServerResponse.createBySuccessMsg("Updated successfully");
        }
        return ServerResponse.createByErrorMsg("Failed to update");
    }

    @Override
    public ServerResponse delete(Integer id) {
        if (null == id) {
            return ServerResponse.createByErrorMsg("id cannot be empty.");
        }
        int count = domainConfigMapper.deleteById(id);
        if (count > 0) {
            return ServerResponse.createBySuccessMsg("Deleted successfully");
        }
        return ServerResponse.createByErrorMsg("Failed to delete");
    }


    @Override
    public ServerResponse listDomain() {
        List<DomainConfig> configs = domainConfigMapper.selectList(new LambdaQueryWrapper<DomainConfig>().eq(DomainConfig::getIsEffect, "1"));
        return ServerResponse.createBySuccess(configs);
    }
}
